package com.hyt.it.ogt.kq.main.controller.gov;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.util.StringUtils;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.exception.GovInvalidRequestException;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.gov.model.entity.TaskArea;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeAreaService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 区域信息表 前端控制器
 * </p>
 *
 * @author huangyh
 * @since 2021-03-08
 */
@RestController
@Api(tags = "122.区域接口", value = "区域接口")
@ApiSort(value = 122)
@RequestMapping("/kw/gov/area")
@Slf4j
public class AreaController extends BaseController {

    @Resource
    private IOfficeAreaService iOfficeAreaService;
    @Resource
    private ITaskAreaService iTaskAreaService;

    @ApiOperation(
            value = "22.1 查询机构下省级区域",
            notes = "30000:查询省级区域成功; 30070:查询省级区域异常"
    )
    @ApiOperationSort(value = 1)
    @RequestMapping(value = "/getProvince", method = RequestMethod.GET)
    public ApiResponse<Object> getProvince() {
        try {
            String officeId = getOfficeId();
            QueryWrapper<TaskArea> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(TaskArea::getType, "2")
                    .eq(TaskArea::getDelFlag, false)
                    .orderByAsc(TaskArea::getAreaId);
            List<TaskArea> officeAreaList = iTaskAreaService.list(queryWrapper);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(officeAreaList)
                    .build();
        } catch (KqException kwGovException) {
            log.error("# 查询省级区域异常: ", kwGovException.getErrMsg());
            return ApiResponse.builder()
                    .code(kwGovException.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 查询省级区域异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_QUERY_PROVINCE_AREA_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "22.2 根据省级id查询机构下市级区域",
            notes = "30000:根据省级id查询市级区域成功; 30071:根据省级id查询市级区域异常"
    )
    @ApiOperationSort(value = 2)
    @RequestMapping(value = "/getCityByProvince", method = RequestMethod.GET)
    public ApiResponse<Object> getCityByProvince(String provinceId) {
        log.info("# 根据省级id查询市级区域参数 ： {}", provinceId);
        try {

            QueryWrapper<TaskArea> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(TaskArea::getType, "3")
                    .eq(TaskArea::getParentId, provinceId)
                    .eq(TaskArea::getDelFlag, false)
                    .orderByAsc(TaskArea::getAreaId);
            List<TaskArea> officeAreaList = iTaskAreaService.list(queryWrapper);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(officeAreaList)
                    .build();
        } catch (KqException kwGovException) {
            log.error("# 根据省级id查询市级区域异常: ", kwGovException.getErrMsg());
            return ApiResponse.builder()
                    .code(kwGovException.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 根据省级id查询市级区域异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_QUERY_CITY_BY_ID_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "22.3 根据市级id查询机构下区县级区域",
            notes = "30000:根据市级id查询区县级区域成功; 30072:根据市级id查询区县级区域异常"
    )
    @ApiOperationSort(value = 3)
    @RequestMapping(value = "/getDistrictByCity", method = RequestMethod.GET)
    public ApiResponse<Object> getDistrictByCity(String cityId) {
        log.info("# 根据市级id查询区县级区域参数 ： {}", cityId);
        try {
            QueryWrapper<TaskArea> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(TaskArea::getType, "4")
                    .eq(TaskArea::getParentId, cityId)
                    .eq(TaskArea::getDelFlag, false)
                    .orderByAsc(TaskArea::getAreaId);
            List<TaskArea> officeAreaList = iTaskAreaService.list(queryWrapper);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(officeAreaList)
                    .build();
        } catch (KqException kwGovException) {
            log.error("# 根据市级id查询区县级区域异常: ", kwGovException.getErrMsg());
            return ApiResponse.builder()
                    .code(kwGovException.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 根据市级id查询区县级区域异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_QUERY_DISTRICT_BY_CITY_ID_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "22.4 根据区域id获取机构下区域名称",
            notes = "30000:根据区域id获取机构下区域名称成功; 30133:根据区域id获取机构下区域名称异常; 30134:区域id不能为空; 30135:区域不存在"
    )
    @ApiOperationSort(value = 4)
    @RequestMapping(value = "/getNameByAreaId", method = RequestMethod.GET)
    public ApiResponse<Object> getNameByAreaId(String areaId) {
        log.info("# 根据区域id获取区域名称参数 ： {}", areaId);
        try {
            if (StringUtils.isEmpty(areaId)) {
                //区域id不能为空
                return ApiResponse.builder()
                        .code(ResponseCode.EXAM_AREA_AREA_ID_IS_NULL.getCode())
                        .build();
            }
            String officeId = getOfficeId();
            String name = iOfficeAreaService.getNameByAreaId(officeId, areaId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(name)
                    .build();
        } catch (KqException kwGovException) {
            log.error("# 根据区域id获取机构下区域名称异常: ", kwGovException.getErrMsg());
            return ApiResponse.builder()
                    .code(kwGovException.getErrCode())
                    .build();
        } catch (GovInvalidRequestException i) {
            log.error("# 根据区域id获取机构下区域名称异常: {}", i.getMessage());
            return ApiResponse.builder()
                    .code(i.getCode())
                    .build();
        } catch (Exception e) {
            log.error("# 根据区域id获取机构下区域名称异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_QUERY_ORGANIZATION_BY_AREA_ID_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "22.5 查询任务下省级区域",
            notes = "30000:查询省级区域成功; 30070:查询省级区域异常"
    )
    @ApiOperationSort(value = 5)
    @GetMapping("/getTaskProvince")
    public ApiResponse<Object> getTaskProvince(String taskId) {
        try {
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(iTaskAreaService.getTaskProvince(taskId))
                    .build();
        } catch (KqException kwGovException) {
            log.error("# 查询省级区域异常: ", kwGovException.getErrMsg());
            return ApiResponse.builder()
                    .code(kwGovException.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 查询省级区域异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_QUERY_TASK_PROVINCE_AREA_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "22.6 根据省级id查询任务下市级区域",
            notes = "30000:根据省级id查询市级区域成功; 30071:根据省级id查询市级区域异常"
    )
    @ApiOperationSort(value = 6)
    @GetMapping("/getTaskCityByProvince")
    public ApiResponse<Object> getTaskCityByProvince(String taskId,String provinceId) {
        log.info("# 根据省级id查询市级区域参数 ： {}", provinceId);
        try {

            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(iTaskAreaService.getTaskCityByProvince(taskId, provinceId))
                    .build();
        } catch (KqException kwGovException) {
            log.error("# 根据省级id查询市级区域异常: ", kwGovException.getErrMsg());
            return ApiResponse.builder()
                    .code(kwGovException.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 根据省级id查询市级区域异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_QUERY_TASK_CITY_BY_ID_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "22.7 根据市级id查询任务下区县级区域",
            notes = "30000:根据市级id查询区县级区域成功; 30072:根据市级id查询区县级区域异常"
    )
    @ApiOperationSort(value = 7)
    @GetMapping("/getTaskDistrictByCity")
    public ApiResponse<Object> getTaskDistrictByCity(String taskId,String cityId) {
        log.info("# 根据市级id查询区县级区域参数 ： {}", cityId);
        try {

            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(iTaskAreaService.getTaskDistrictByCity(taskId, cityId))
                    .build();
        } catch (KqException kwGovException) {
            log.error("# 根据市级id查询区县级区域异常: ", kwGovException.getErrMsg());
            return ApiResponse.builder()
                    .code(kwGovException.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 根据市级id查询区县级区域异常: ", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAM_AREA_QUERY_DISTRICT_BY_TASK_CITY_ID_EXCEPTION.getCode())
                    .build();
        }
    }

}
